﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.DependencyInjection;
using StyleMVVM.ViewModel;
using System.ComponentModel;
using Wala.Connection.Objects.Timeline;
using System.Collections.ObjectModel;
using Wala.Connection.Objects.Contacts;
using Windows.UI.Popups;
using StyleMVVM.View;
using Wala.Connection.Managements;
using Windows.UI.Xaml.Controls;

namespace Wala.ViewModels.Timelines
{
    public class TimelinePageViewModel : PageViewModel, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region //------------- Private Fields ------------//     

        private ContactBasic profile;
        private ObservableCollection<TimelineInfo> timelines;

        private EventCommand itemClickCommand;

        #endregion

        #region //------------ CLR Properties -------------//

        public ContactBasic Profile
        {
            get
            {
                return profile;
            }
            set
            {
                if (profile != value)
                {
                    profile = value;
                    OnPropertyChanged("Profile");
                }
            }
        }        

        public string DisplayName
        {
            get
            {
                if (profile != null)
                {
                    return profile.Friend_display_name;
                }
                return "";
            }
        }

        public ObservableCollection<TimelineInfo> Timelines
        {
            get { return timelines; }
            set
            {
                if (timelines != value)
                {
                    timelines = value;
                    OnPropertyChanged("Timelines");
                }
            }
        }

        public EventCommand ItemClickCommand
        {
            get
            {
                if (itemClickCommand == null)
                {
                    itemClickCommand = new EventCommand(itemClickHandler);
                }
                return itemClickCommand;
            }
        }

        #endregion

        #region //---------- Public Methods ------------

        [ActivationComplete]
        public async void Active()
        {

        }

        #endregion

        #region //--------- Override Methods ------------//

        protected override async void OnNavigatedTo(object sender, StyleNavigationEventArgs e)
        {
            Profile = NavigationParameter as ContactBasic;
            List<TimelineInfo> lstTimeline = new List<TimelineInfo>();
            lstTimeline = await TimelineManager.GetFriendTimeline(Profile.Friend_id, 0, 9);
            Timelines = new ObservableCollection<TimelineInfo>();
            foreach (TimelineInfo timeline in lstTimeline)
            {
                Timelines.Add(timeline);
            }
        }

        #endregion

        #region //----------- Protected Methods ----------//

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region //----------- Private Methods ---------//

        private async void LikeClickHanlder(TimelineInfo dataContext)
        {
            bool success = false;

            if (dataContext.User_Like)
            {
                // Pre-showing to user
                dataContext.User_Like = false;
                dataContext.Number_Of_Likes -= 1;

                success = await TimelineManager.DoUnLike(dataContext.Id, dataContext.Type, dataContext.Location_X.ToString(), dataContext.Location_Y.ToString());

                if (!success)
                {
                    dataContext.User_Like = true;
                    dataContext.Number_Of_Likes += 1;
                }
            }
            else
            {
                // Pre-show to user
                dataContext.User_Like = true;
                dataContext.Number_Of_Likes += 1;

                success = await TimelineManager.DoLike(dataContext.Id, dataContext.Type, dataContext.Location_X.ToString(), dataContext.Location_Y.ToString());

                if (!success)
                {
                    dataContext.User_Like = false;
                    dataContext.Number_Of_Likes -= 1;
                }
            }
        }

        private void itemClickHandler(object sender, object args, object parameter)
        {
            ItemClickEventArgs clickEventArgs = args as ItemClickEventArgs;

            if (clickEventArgs != null)
            {
                TimelineInfo item = ((TimelineInfo)clickEventArgs.ClickedItem);
                Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("Contact", Profile);
                dic.Add("CurTimeline",item);
                Navigation.Navigate("TimelineDetailPage", dic);                
            }
            else
            {
                Log.Error("Missed ItemClickEventArgs args");
            }
        }

        #endregion
    }
}
